package com.ruoyi.web.controller.kong;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.kong.bean.NextStepBean;
import com.ruoyi.kong.domain.KSbh;
import com.ruoyi.kong.mapper.KSbhMapper;
import com.ruoyi.kong.service.IKStepService;
import com.ruoyi.socket.ws.WsSysMsg;
import com.ruoyi.socket.ws.bean.WsMsg;
import com.ruoyi.web.controller.kong.vo.GoLiveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/kong/dy")
public class KongController extends BaseController {

    @Autowired
    private IKStepService kStepService;

    @Autowired
    KSbhMapper kSbhMapper;

    @PostMapping("/goLive")
    public AjaxResult goLive(@RequestBody GoLiveVo vo){
        WsMsg<NextStepBean> msg = new WsMsg<>();
        if(vo.getActype()==-2){
            msg.setAc(-2);
        }else{
            NextStepBean nextStepBean = kStepService.selectNextStepBean(vo.getCmdSetGid(),vo.getParams());
            msg.setData(nextStepBean);
            msg.setAc(1);
        }

        List<KSbh> sbhList =  kSbhMapper.selectKSbhGroupList(vo.getGroupNames());
        if(sbhList.isEmpty()){
            return toAjax(false);
        }
        List<String> gids = sbhList.stream().map(KSbh::getGid).map(Object::toString).collect(Collectors.toList());



        WsSysMsg.sendMsgs(gids, JSON.toJSONString(msg));
        return toAjax(true);
    }
}
